#region File Description
//-----------------------------------------------------------------------------
// PrimitivesSampleGame.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
#endregion

namespace Zephyris_Space_Mercs
{
    // This sample illustrates the use of PrimitiveBatch to draw lines and points
    // on the screen. Lines and points are used to recreate the Spacewars starter kit's
    // retro mode.
    public class StarField
    {
        #region Constants

        // this constant controls the number of stars that will be created when the game
        // starts up.
        const int NumStars = 50;

        // how bright will stars be?  somewhere between these two values.
        const byte MinimumStarBrightness = 255;
        const byte MaximumStarBrightness = 255;

        #endregion

        #region Fields

        public Vector3 currPos;
        public bool needsReset = true;

        // PrimitiveBatch is the new class introduced in this sample. We'll use it to
        // draw everything in this sample, including the stars, ships, and sun.
        PrimitiveBatch primitiveBatch;

        // these two lists, stars, and starColors, keep track of the positions and
        // colors of all the stars that we will randomly generate during the initialize
        // phase.
        List<Vector3> stars = new List<Vector3>();
        List<Color> starColors = new List<Color>();

        Random rndX = new Random(1);
        Random rndY = new Random(90);
        Random rndZ = new Random(13);

        Texture2D starsTex;

        #endregion


        public void InitStars(GraphicsDevice device, Texture2D texture)
        {
            this.primitiveBatch = new PrimitiveBatch(device);
            this.starsTex = texture;
        }

        public void ClearStars()
        {
            stars.Clear();
            starColors.Clear();
        }

        public void CreateStars()
        {
            // since every star will be put in a random place and have a random color, 
            // a random number generator might come in handy.
            
            if (needsReset)
            {
                ClearStars();

                Random random = new Random();
                Vector3 starPos = this.currPos;

                for (int i = 0; i < NumStars; i++)
                {
                    // pick a random spot...
                    Vector3 where = new Vector3();

                    where.X = (rndX.Next(-20, 20) * 0.1f) + starPos.X;
                    where.Y = (rndY.Next(-20, 20) * 0.1f) + starPos.Y;
                    where.Z = (rndZ.Next(-20, 20) * 0.1f) + starPos.Z;

                    // ...and a random color. it's safe to cast random.Next to a byte,
                    // because MinimumStarBrightness and MaximumStarBrightness are both
                    // bytes.
                    byte greyValue =
                        (byte)random.Next(MinimumStarBrightness, MaximumStarBrightness);
                    Color color = new Color(greyValue, greyValue, greyValue);


                    starColors.Add(color);
                    stars.Add(where);

                }

                needsReset = false;
            }
        }


        // DrawStars is called to do exactly what its name says: draw the stars.
        public void DrawStars(Matrix viewMatrix, Matrix projectionMatrix)
        {
            // stars are drawn as a list of points, so begin the primitiveBatch.
            primitiveBatch.Begin(PrimitiveType.TriangleStrip, viewMatrix, projectionMatrix, this.starsTex);

            // loop through all of the stars, and tell primitive batch to draw them.
            // each star is a very small triangle.
            for (int i = 0; i < stars.Count; i++)
            {
                primitiveBatch.AddVertex(stars[i], starColors[i]);
                primitiveBatch.AddVertex(stars[i] + new Vector3(0.01f, 0, 0), starColors[i]);
                primitiveBatch.AddVertex(stars[i] + new Vector3(0, 0.01f, 0), starColors[i]);
                primitiveBatch.AddVertex(stars[i] + new Vector3(0.01f, 0.01f, 0), starColors[i]);
            }

            // and then tell it that we're done.
            primitiveBatch.End();
        }

    }
}
